﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DreamBox.Net.Data;

namespace DreamBox.Net.Core
{
    public class DbToJson
    {
        /// <summary>
        /// 哈希表转JSON
        /// </summary>
        /// <param name="hash"></param>
        /// <returns></returns>
        public string HashTableToJson(GHashTable hash)
        {
            StringBuilder json = new StringBuilder();
            json.Append("{");

            int count = 0;
            foreach (object obj in hash.Keys)
            {
                json.Append("'");
                json.Append(obj.ToString());
                json.Append("':'");
                json.Append(hash[obj].ToString());
                json.Append("'");
                if (count < hash.Count-1)
                {
                    json.Append(",");
                }
                count++;
            }
            json.Append("}");
            return json.ToString();
        }
        /// <summary>
        /// DataTable转JSON 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public string ToJson(DataTable dt)
        {
            StringBuilder json = new StringBuilder();
            if (dt.Rows.Count > 0)
            {
                json.Append("{");
                json.Append("'data':[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        json.Append("'");
                        json.Append(dt.Columns[j].ColumnName);
                        json.Append("':'");
                        json.Append(Replace(dt.Rows[i][j].ToString()));
                        json.Append("'");
                        if (j < dt.Columns.Count - 1)
                        {
                            json.Append(",");
                        }

                    }
                    json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        json.Append(",");
                    }
                }
                json.Append("]}");
            }
            else
            {
                json.Append("");
            }
            return json.ToString();
        }
        /// <summary>
        /// DbTable 转Json
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public string GetJson(DbTable dt)
        {
            StringBuilder json = new StringBuilder();
            if (dt.Rows.Count > 0)
            {
                json.Append("[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    json.Append("{");
                    for (int j = 0; j < dt.Rows[i].Columns.Count; j++)
                    {
                        json.Append("'");
                        json.Append(dt.Rows[i].Columns[j].Name);
                        json.Append("':'");
                        json.Append(Replace(dt.Rows[i][j].ToString()));
                        json.Append("'");
                        if (j < dt.Rows[i].Columns.Count - 1)
                        {
                            json.Append(",");
                        }
                    }
                    json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        json.Append(",");
                    }
                }
                json.Append("]");
            }
            else
            {
                json.Append("");
            }
            return json.ToString();
        }
        string Replace(string Contents)
        {
            Contents = Contents.Replace("\\", "\\\\");
            Contents = Contents.Replace("{", "\\{");
            Contents = Contents.Replace("}", "\\}");
            Contents = Contents.Replace("[", "\\[");
            Contents = Contents.Replace("]", "\\]");
            Contents = Contents.Replace("\"", "\\\"");
            Contents = Contents.Replace("'", "\\\'");
            Contents = Contents.Replace(",", "\\,");
            Contents = Contents.Replace(":", "\\:");
            return Contents;
        }
    }
}
